Page({

  data: {
    showTopTips: false
  },


  showTopTips: function() {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 1500);
  },


  sub: function(e) {
    if (e.detail.value.input == '') {
      this.showTopTips();
      return;
    }
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/settings/buyer/addbuyer/uniquecheck.php',
          data: {
            name: e.detail.value.input,
            openid: res.data
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            if (!res.data) {
              wx.showModal({
                title: '名字重复',
                content: '已经有重复的名字了，确认仍要提交吗？',
                confirmText: "确认",
                cancelText: "取消",

                confirmColor: '#ffd948',
                success: function(res) {
                  if (res.confirm) {
                    that.add(e)
                  }
                }
              })
            }
            else
              that.add(e)
          }

        })
      },
    })
  },

  add: function(e) {
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/settings/buyer/addbuyer/addbuyer.php',
          data: {
            name: e.detail.value.input,
            openid: res.data
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log(res)
          }

        })
      },
    })
    wx.showToast({
      title: '添加成功',
    })
    setTimeout(function() {
      wx.redirectTo({
        url: '../buyermanagement/buyermanagement',
      })
    }, 1000)


  }




});